﻿using System;
using System.Web.UI.WebControls;
using Controllers;
using System.Linq;

namespace Web
{
    public partial class DutyList : System.Web.UI.Page
    {
        public Pagination Model;
        private IQueryable<DutyDTO> listData;
        protected void Page_Load(object sender, EventArgs e)
        {
            Model = new Pagination();
            Model.PageNumShown = 10;
            Model.CurrentPage = 1;
            if (Request.QueryString["pageNum"] != null)
            {
                Model.CurrentPage = int.Parse(Request.QueryString["pageNum"]);
            }
            Model.NumPerPage = 5;
            if (Request.QueryString["numPerPage"] != null)
            {
                Model.NumPerPage = int.Parse(Request.QueryString["numPerPage"]);
            }

            DutyController duty=new DutyController();
            int cnt;
            int start = (Model.CurrentPage - 1)*Model.NumPerPage;
            listData = duty.Find(start, Model.NumPerPage, out cnt);
            Model.TotalCount = cnt;

            rptList.DataSource = listData.Select(r => r.DutyDate).Distinct().OrderByDescending(r=>r.Date);
            rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rptGaoGuan = (Repeater)e.Item.FindControl("rptGaoGuan");
                Repeater rptJingLi = (Repeater)e.Item.FindControl("rptJingLi");
                Repeater rptYeWu = (Repeater)e.Item.FindControl("rptYeWu");
                Repeater rptXingZheng = (Repeater)e.Item.FindControl("rptXingZheng");
                Repeater rptJieSuan = (Repeater)e.Item.FindControl("rptJieSuan");
                Repeater rptWuLiu = (Repeater)e.Item.FindControl("rptWuLiu");
                Repeater rptCaiWu = (Repeater)e.Item.FindControl("rptCaiWu");
                Repeater rptChanPing = (Repeater)e.Item.FindControl("rptChanPing");
                //Repeater rptZhiYing = (Repeater)e.Item.FindControl("rptZhiYing");
                Repeater rptHuiYi = (Repeater)e.Item.FindControl("rptHuiYi");

                DateTime dutyDate = (DateTime)e.Item.DataItem;

                rptGaoGuan.DataSource = listData.Where(r => r.DeptId == -1 && r.DutyDate==dutyDate);//高管
                rptGaoGuan.DataBind();

                rptJingLi.DataSource = listData.Where(r => r.DeptId == -2 && r.DutyDate == dutyDate);//值班经理
                rptJingLi.DataBind();

                rptXingZheng.DataSource = listData.Where(r => r.DeptId == 21 && r.DutyDate == dutyDate);//行政中心
                rptXingZheng.DataBind();

                rptJieSuan.DataSource = listData.Where(r => r.DeptId == 17 && r.DutyDate == dutyDate);//结算部
                rptJieSuan.DataBind();

                rptWuLiu.DataSource = listData.Where(r => r.DeptId == 6&& r.DutyDate == dutyDate);//物流中心
                rptWuLiu.DataBind();

                rptCaiWu.DataSource = listData.Where(r => r.DeptId == 35 && r.DutyDate == dutyDate);//财务中心
                rptCaiWu.DataBind();

                rptYeWu.DataSource = listData.Where(r => r.DeptId == 18 && r.DutyDate == dutyDate);//业务部
                rptYeWu.DataBind();

                rptChanPing.DataSource = listData.Where(r => r.DeptId == 16 && r.DutyDate == dutyDate);//产品部
                rptChanPing.DataBind();

                //rptZhiYing.DataSource = listData.Where(r => r.DeptId == 19 && r.DutyDate == dutyDate);//直营店
                //rptZhiYing.DataBind();

                rptHuiYi.DataSource = listData.Where(r => r.DeptId == 37 && r.DutyDate == dutyDate);//会议管理部
                rptHuiYi.DataBind();
            }
        }
    }
}